package rentapartments.xwj.com.rentapartments.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.jaeger.library.StatusBarUtil;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.SupportFragment;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;
import rentapartments.xwj.com.rentapartments.R;
import rentapartments.xwj.com.rentapartments.fragment.MyChatFragment;
import rentapartments.xwj.com.rentapartments.fragment.MyHomeFragment;
import rentapartments.xwj.com.rentapartments.fragment.MyMapFragment;
import rentapartments.xwj.com.rentapartments.fragment.MyPersonFragment;
import rentapartments.xwj.com.rentapartments.fragment.MySettingFragment;

public class MainActivity extends SupportActivity {

    private AccountHeader headerResult;
    private SupportFragment[] mFragments=new SupportFragment[5];
    private Drawer drawer;
    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;  //点击返回键时间
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(MainActivity.this);
        setContentView(R.layout.activity_main);
        InitHeadLayout();
        InitDrawerBuilder(savedInstanceState);//侧边栏
        if(findFragment(MyMapFragment.class) == null){
            mFragments[0]=MyMapFragment.newInstance();
            mFragments[1]= MyPersonFragment.newInstance();
            mFragments[2]= MySettingFragment.newInstance();
            mFragments[3]= MyHomeFragment.newInstance();
            mFragments[4]= MyChatFragment.newInstance();
            loadMultipleRootFragment(R.id.fl_container,0,mFragments[0],
                    mFragments[1],mFragments[2],mFragments[3],mFragments[4] );
        }else {
            mFragments[0]=findFragment(MyMapFragment.class);
            mFragments[1]= findFragment(MyPersonFragment.class);
            mFragments[2]= findFragment(MySettingFragment.class);
            mFragments[3]= findFragment(MyHomeFragment.class);
            mFragments[4]= findFragment(MyChatFragment.class);
        }
    }

    /**
     * 初始化侧边栏
     * @param savedInstanceState
     */
    private void InitDrawerBuilder(Bundle savedInstanceState) {
        PrimaryDrawerItem item0 = new PrimaryDrawerItem().withIdentifier(1).withName("首页").withIcon(R.mipmap.ic_launcher);
        PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(2).withName("个人中心").withIcon(R.mipmap.ic_launcher);
        PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(3).withName("设置").withIcon(R.mipmap.ic_launcher);
        PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(4).withName("我的小窝").withIcon(R.mipmap.ic_launcher);
        PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(5).withName("聊聊").withIcon(R.mipmap.ic_launcher);
        drawer= new DrawerBuilder()//滑动抽屉风格
                .withActivity(this)
                .withAccountHeader(headerResult)
                .withHeaderDivider(false)
                .withSavedInstance(savedInstanceState)
                .withTranslucentStatusBar(true)//启用沉浸式状态栏
                .addDrawerItems(item0,item1, item2,item3,item4)
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        showHideFragment(mFragments[position-1]);//切换fragment
                        return false;
                    }
                })
                .build();
    }

    /**
     * 设置侧边栏头部
     */
    private void InitHeadLayout(){
        headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.color.colorAccent)
                .addProfiles(//添加用户对象：可以添加多个，以逗号隔开
                        new ProfileDrawerItem().withName("许伟杰").withEmail("541535798@qq.com").withIcon(R.mipmap.ic_launcher)
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();
    }

    /**
     * 设置所有Fragment的出场消失动画
     * @return
     */
    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        return super.onCreateFragmentAnimator();//竖向动画
    }

    /**
     * 手机返回键
     */
    @Override
    public void onBackPressedSupport() {
        if(drawer.isDrawerOpen()){
            drawer.closeDrawer();
        }else {
            if(getSupportFragmentManager().getBackStackEntryCount()>1){
                pop();
            }else{
                if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                    finish();
                } else {
                    TOUCH_TIME = System.currentTimeMillis();
                    Toast.makeText(this,"再按一次退出程序", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }
}
